package com.mouse.hxsj.constants;

import com.mouse.damo.micro.Com;
import com.mouse.damo.micro.Sleep;
import com.mouse.damo.micro.Window;
import com.mouse.hxsj.util.StringUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

/**
 * 游戏
 * 绑定句柄的Com池
 */
@Slf4j
public class ComPool {
    @Getter
    public static List<Com> comList = new CopyOnWriteArrayList<>();//Com index一样
    @Getter
    public static List<String> accountList = new CopyOnWriteArrayList<>();//账号 index一样
        public final static String gameName = "QQ幻想世界";
//    public final static String gameName = "Navicat Premium";

    public static Com getByHwmd(Integer hwnd) {
        if (hwnd == null) return null;
        List<Com> list = comList.stream().filter(com -> com.getHwnd() == hwnd).collect(Collectors.toList());
        if (list.size() > 0) return list.get(0);
        return null;
    }

    //初始化线程检测句柄
    public static void init() {
        comList.add(new Com());
        accountList.add("893656121");
        Com com1 = new Com();
        com1.setHwnd(1381894);
        comList.add(com1);
        accountList.add("程序");
        new Thread(() -> {
            Com checkCom = new Com();
            while (true) {
                checkNewHwnd(checkCom);
                Sleep.sleep(30 * 1000);
            }
        }).start();
        new Thread(() -> {
            while (true) {
                comList.forEach(com -> destoryHwnd(com));
                Sleep.sleep(30 * 1000);
            }
        }).start();
    }

    public static synchronized void add(Com com, String account) {
        log.info("进入Com池,句柄:{},账号:{}", com.getActiveDm(), account);
        if (com.getHwnd() == 0) return;
        for (Com com1 : comList) {
            if (com1.getHwnd() == com.getHwnd()) {
                comList.remove(com1);
                com1.unbind();
                com1.destroy();
                break;
            }
        }
        comList.add(com);
        accountList.add(account);
    }

    public static synchronized Com create(int hwnd) {
        log.info("自动创建句柄:{}", hwnd);
        Com com = new Com();
        com.bind(hwnd);
        com.setResourcePath(SystemParams.sourcePath);
        return com;
    }

    public static synchronized Com get(int hwnd) {
        List<Com> list = comList.stream().filter(com -> com.getHwnd() == hwnd).collect(Collectors.toList());
        if (list.size() > 0) return list.get(0);
        log.info("自动创建句柄:{}", hwnd);
        Com com = new Com();
        com.bind(hwnd);
        com.setResourcePath(SystemParams.sourcePath);
        comList.add(com);
        accountList.add("0");
        return com;
    }

    public synchronized static void destoryHwnd(Com com) {
        Window window = new Window(com);
        boolean exist = window.windowIsExist(com.getHwnd());
        if (exist) return;
        String windowTitle = window.getWindowTitle(com.getHwnd());
        if (gameName.equalsIgnoreCase(windowTitle)) return;
        com.unbind();
        com.destroy();
        int index = comList.indexOf(com);
        log.info("解绑句柄:{},账号:{}", com.getActiveDm(), accountList.get(index));
        comList.remove(com);
        accountList.remove(index);
    }

    private synchronized static void checkNewHwnd() {
        checkNewHwnd(new Com());
    }

    private synchronized static void checkNewHwnd(Com checkCom) {
        Window window = new Window(checkCom);
        String hwndsStr = window.findWindows(gameName);
        if (StringUtil.isBlank(hwndsStr)) return;
        Arrays.stream(hwndsStr.split(",")).forEach(s -> {
            Integer hwnd = Integer.valueOf(s);
            String windowTitle = window.getWindowTitle(hwnd);
            if (!gameName.equals(windowTitle)) return;
            boolean anyMatch = comList.stream().anyMatch(com -> com.getHwnd() == hwnd);
            if (anyMatch) return;
            log.info("非自动登录句柄:{}", hwnd);
            Com com = new Com();
            com.bind(hwnd);
            com.setResourcePath(SystemParams.sourcePath);
            comList.add(com);
            accountList.add("0");
        });
    }
}
